package org.spring.springboot.server.Impl;

import org.spring.springboot.dao.VisitorSubscribeCommitDao;
import org.spring.springboot.dao.VisitorSubscribeDao;
import org.spring.springboot.entity.VisitorSubscribeCommit;
import org.spring.springboot.entity.VisitorTeg;
import org.spring.springboot.server.IntervieweeManageService;
import org.spring.springboot.server.VisitorSubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;

@Service
public class VisitorSubscribeServiceImpl implements VisitorSubscribeService {
    @Autowired
    private VisitorSubscribeDao visitorSubscribeDao;

    @Autowired
    private VisitorSubscribeCommitDao visitorSubscribeCommitDao;

    @Autowired
    private IntervieweeManageService intervieweeManageService;

    @Override
    public List<Map<String, Object>> intervieweeName1(String name) {
        List<String> userNameList = intervieweeManageService.getUserNameList();
        int count = 0;
        for(int i = 0; i < userNameList.size(); i ++){
            String str = userNameList.get(i);
            if(name.equals(str)){
                count++;
            }
        }
        if (count == 1){
            return intervieweeManageService.selectmobile(name);
        }
        if (count > 1){
            ArrayList<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> map=new HashMap<String, Object>();
            map.put(name,"被访者重名");
            list.add(map);
            return list;
        }
        else {
            ArrayList<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> map=new HashMap<String, Object>();
            map.put(name,"被访者不存在");
            list.add(map);
            return list;
        }
    }

    @Override
    public List<Map<String, Object>> intervieweeName2(String name,String mobile) {
        List<String> userNameList = intervieweeManageService.getUserNameList();
        List<Map<String, Object>> usermobileList = intervieweeManageService.selectmobile(name);
        List<String> mobileList = new ArrayList<>();
        for (Map<String,Object> map: usermobileList){
            //keySet()方法，获取map集合的所有键的set集合
            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()){
                String key = it.next();//迭代器取出key值
                mobileList.add((String) map.get(key));//根据key值获取相应的value值放入集合list
            }
        }
        boolean contains = mobileList.contains(mobile);
        int count = 0;
        for(int i = 0; i < userNameList.size(); i ++){
            String str = userNameList.get(i);
            if(name.equals(str)){
                count++;
            }
        }
        if (count > 1) {
            if (contains==true){
                return intervieweeManageService.selectinterviewee(name, mobile);
            }
            else {
                ArrayList<Map<String, Object>> list = new ArrayList<>();
                Map<String, Object> map=new HashMap<String, Object>();
                map.put("mobile","手机号与姓名不匹配");
                list.add(map);
                return list;
            }
        }
        else {
            ArrayList<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("name","被访者不重复或不存在");
            list.add(map);
            return list;
        }
    }

    @Override
    public List<Map<String, Object>> selectSubscribeStatus(String visitorname) {
        return visitorSubscribeDao.selectByStatus(visitorname);
    }

    @Override
    public List<Map<String, Object>> selectVisitorByFid(Integer fid) {
        return visitorSubscribeDao.selectvisitorTeg(fid);
    }

    @Override
    public List<Map<String, Object>> selectSubscribeNum() {
        return visitorSubscribeDao.selectBysubscribeNum();
    }

    @Override
    public List<Map<String, Object>> selectSubscribe1Num() {
        return visitorSubscribeDao.selectBysubscribe1Num();
    }

    @Override
    public List<Map<String, Object>> selectSubscribe2Num() {
        return visitorSubscribeDao.selectBysubscribe2Num();
    }

    @Override
    public List<Map<String, Object>> selectSubDetail() {
        return visitorSubscribeDao.selectBysubscribeDetail();
    }

    @Override
    public void addSubscribe(VisitorSubscribeCommit vs) {
        visitorSubscribeCommitDao.insert(vs);
    }

    @Override
    public void addSubscribeTeg(VisitorTeg vt) {
        visitorSubscribeCommitDao.insert2(vt);
    }

    @Override
    public int selectId() {
        return visitorSubscribeCommitDao.select();
    }

    @Override
    public List<Map<String, Object>> selectBysubscribe(String visitorname) {
        return visitorSubscribeDao.selectBysubscribe(visitorname);
    }

    @Override
    public List<Map<String, Object>> selectBysubscribe1(String visitorname) {
        return visitorSubscribeDao.selectBysubscribe1(visitorname);
    }

    @Override
    public List<Map<String, Object>> selectBysubscribe2(String visitorname) {
        return visitorSubscribeDao.selectBysubscribe2(visitorname);
    }

    @Override
    public void interviewee_manage(Integer intervieweeId,Integer id,String remark1) {
        visitorSubscribeDao.interviewee_manage(intervieweeId, id ,remark1);
    }

    @Override
    public void department_manage(Integer departmentId,String departmentName,Integer id,String remark2) {
        String fname = visitorSubscribeDao.selectIntervieweeName(id);
        Integer fid = new Integer(0);
        if (fname.equals(departmentName)){
            fid = departmentId;
            visitorSubscribeDao.interviewee_manage(fid, id,"");
        }
        visitorSubscribeDao.department_manage(departmentId, departmentName, id ,remark2);
    }

    @Override
    public void subscribe_complete(Integer complete,Integer id) {
        visitorSubscribeDao.subscribe_complete(complete,id);
    }

    @Override
    public List<Map<String, Object>> selectByExitlist(Integer id) {
        return visitorSubscribeDao.selectInvite(id);
    }

    @Override
    public List<Map<String, Object>> selectByRecord(Timestamp starttime, Timestamp endtime,Integer id) {
        return visitorSubscribeDao.selectHistoryRecord(starttime, endtime,id);
    }

}
